#!/usr/bin/env python
# coding=utf8


import sys

sys.path.append('..')
sys.path.append('../../')
import json
import traceback
import time
from pymongo import MongoClient
import hashlib
from bson.objectid import ObjectId


def get_src_company_list():
    host = '101.201.100.58'
    port = 27017
    database = 'crawl_data'
    coll = 'enterprise_data_gov_haizhi'
    client = MongoClient(host, port)
    db = client[database][coll]
    feild = {'_id': 1, 'company_name': 1}
    cursor = db.find({}, feild)
    num = 0

    begin_time = time.time()
    src_company_list = []
    for item in cursor:
        try:
            num += 1
            company = item.get("_id", "")
            if isinstance(company, ObjectId):
                company = item.get("company_name", "")
            if not company or isinstance(company, ObjectId):
                company = ''

            src_company_list.append(company)
            if num % 500 == 0:
                print num, "time_cost:", time.time() - begin_time

        except:
            print traceback.format_exc()

    return src_company_list


def get_target_company_list():
    host = '101.201.102.37'
    port = 27017
    database = 'final_data'
    coll = 'enterprise_data_gov'
    client = MongoClient(host, port)
    db = client[database][coll]
    feild = {'_id': 1, 'company': 1}
    cursor = db.find({}, feild)
    num = 0

    begin_time = time.time()
    company_list = []
    for item in cursor:
        try:
            num += 1
            company = item.get("company", "")

            company_list.append(company)
            if num % 500 == 0:
                print num, "time_cost:", time.time() - begin_time
        except:
            print traceback.format_exc()

    return company_list


def get_diff_company():
    src_company_list = get_src_company_list()
    print "finish src_company"
    company_list = get_target_company_list()
    print "finish target_company"
    f = open('company_list.txt', 'w')
    for company in src_company_list:
        if company not in company_list:
            f.write(company)
            f.write('\n')
    f.close()

    return


if __name__ == "__main__":
    print "start"
    get_diff_company()
